package com.ufgov.agcfs.baseData.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ufgov.agcfs.baseData.entity.AgcfsSysItem;
import com.ufgov.agcfs.baseData.entity.Eacc;
import com.ufgov.agcfs.baseData.service.AgcfsItemService;

import uap.iweb.entity.DataTable;
import uap.iweb.entity.Row;
import uap.iweb.exception.WebRuntimeException;

@Controller("agcfs.base.agcfsItemController")
@Scope("prototype")
@RequestMapping(value = "/baseData/reportItem")
public class AgcfsItemController {
	private final Logger logger = LoggerFactory.getLogger(AgcfsItemController.class);
	
	DataTable<AgcfsSysItem> reportItemDataTable;
	
	@Autowired
	AgcfsItemService itemService;
	
	public void loadItemByNd() {
		try {
			Map<String, Object> searchParams = new HashMap<String, Object>();
			searchParams = itemService.createSearchParamsStartingWith("search_",reportItemDataTable);
			String nd=(String)searchParams.get("EQ_nd");
			List<AgcfsSysItem> listAgcfsItem = itemService.getItemByNd(nd);
			
			reportItemDataTable.remove(reportItemDataTable.getAllRow());
			reportItemDataTable.set(listAgcfsItem.toArray(new AgcfsSysItem[0]));
		} catch (Exception e) {
			logger.error("查询数据失败!", e);
			throw new WebRuntimeException("查询数据失败!");
		}
	}

	//批量保存
	public void saveItemByNd() {
		try {
			Map<String, Object> searchParams = new HashMap<String, Object>();
			searchParams = itemService.createSearchParamsStartingWith("search_",reportItemDataTable);
			String nd=(String)searchParams.get("EQ_nd");
			itemService.saveItem(nd, reportItemDataTable);
		} catch (Exception e) {
			logger.error("保存数据失败!", e);
			throw new WebRuntimeException("保存数据失败!");
		}
	}
	
	@RequestMapping(method = RequestMethod.POST,value="saveReportItem")
	public  @ResponseBody Map<String, String> saveReportItem(@RequestBody AgcfsSysItem item,HttpServletRequest request, HttpServletResponse response){
		Map<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("result", "success");
		resultMap.put("reason", "保存成功！");
		try {
			itemService.saveOneItem(item,resultMap);
		} catch (Exception e) {
			resultMap.put("result", "fail");
			resultMap.put("reason", "保存失败，请稍后重试！");
		}
		return resultMap;
	}
	
	@RequestMapping(method = RequestMethod.POST,value="deleteReportItem")
	public @ResponseBody Map<String, String> deleteReportItem(HttpServletRequest request, HttpServletResponse response){
		Map<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("result", "success");
		resultMap.put("reason", "删除成功！");
		try {
			String id = request.getParameter("id");
			AgcfsSysItem item = new AgcfsSysItem();
			item.setId(id);
			itemService.deleteReportItem(item,resultMap);
		} catch (Exception e) {
			resultMap.put("result", "fail");
			resultMap.put("reason", "服务端繁忙，请稍后重试！");
		}
		return resultMap;		
	}
}
